package com.augurit.common.workdayholiday.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.dao.WhRegularWorkTimeDao;
import com.augurit.common.entity.WhRegularWorkTime;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.workdayholiday.convert.WhRegularWorkTimeConvertor;
import com.augurit.common.workdayholiday.model.Time;
import com.augurit.common.workdayholiday.model.TimeSpan;
import com.augurit.common.workdayholiday.service.IWhRegularWorkTimeService;
import com.augurit.common.workdayholiday.web.form.WhRegularWorkTimeForm;

@Service
@Transactional
public class WhRegularWorkTimeServiceImpl implements IWhRegularWorkTimeService{

	@Autowired
	private WhRegularWorkTimeDao whRegularWorkTimeDao;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public WhRegularWorkTimeForm get(Long id){
		WhRegularWorkTime entity = whRegularWorkTimeDao.get(id);
		return WhRegularWorkTimeConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		whRegularWorkTimeDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(WhRegularWorkTimeForm... forms) {
		if(forms != null)
			for(WhRegularWorkTimeForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(WhRegularWorkTimeForm form){
		
		if(form != null){
			WhRegularWorkTime entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = whRegularWorkTimeDao.get(form.getId());
			}else{
				entity = new WhRegularWorkTime();
			}
			
			//属性值转换
			WhRegularWorkTimeConvertor.convertFormToVo(form, entity);
			
			//保存
			whRegularWorkTimeDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WhRegularWorkTimeForm> search(Page<WhRegularWorkTimeForm> page, WhRegularWorkTimeForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from WhRegularWorkTime ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		whRegularWorkTimeDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WhRegularWorkTimeForm> list = WhRegularWorkTimeConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WhRegularWorkTimeForm> search(Page<WhRegularWorkTimeForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<WhRegularWorkTime> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		whRegularWorkTimeDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WhRegularWorkTimeForm> list = WhRegularWorkTimeConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 清空原来的所有常规工作时间
	 */
	public void clear(){
		List<WhRegularWorkTime> entities = whRegularWorkTimeDao.getAll();
		for(WhRegularWorkTime entity:entities){
			whRegularWorkTimeDao.delete(entity);
		}
	}
	
	/**
	 * 获取常规工作日数组，星期日-->1, 星期一-->2 以此类推
	 * @return
	 */
	public List<String> getRegularWorkdays(){
		String hql = "select ps.workday from WhRegularWorkTime ps";
		return whRegularWorkTimeDao.find(hql);
	}
	
	
	
	/**
	 * 计算一周中某一天的工作时长(即多少个小时，多少分钟)
	 * @param dayOfWeek
	 * @return
	 */
	@Transactional(readOnly = true)
	public  TimeSpan getWorkTimeSpan(int dayOfWeek){
		assert(dayOfWeek >= 1 && dayOfWeek <= 7);
		
		TimeSpan result = new TimeSpan(0, 0);
		WhRegularWorkTime entity = getOneRegularWorkTime(dayOfWeek);
		
		if(entity != null){
			//此处是表示多少分钟多少小时
			TimeSpan am = TimeSpan.calTimeSpan(entity.getAmStartHour(), entity.getAmStartMinute(), entity.getAmEndHour(), entity.getAmEndMinute());
			TimeSpan pm = TimeSpan.calTimeSpan(entity.getPmStartHour(), entity.getPmStartMinute(), entity.getPmEndHour(), entity.getPmEndMinute());
			result = am.add(pm);
		}
		return result;
	}
	
	/**
	 * 计算一周中每天的常规工作时长
	 * @return  
	 */
	@Transactional(readOnly = true)
	public Map<Integer, TimeSpan> getReguarWorkTimeSpan(){
		//次数表示多少分钟多少小时
		Map<Integer, TimeSpan> result = new HashMap<Integer, TimeSpan>();
		for(int i = 1; i < 8; i++){
			result.put(i, getWorkTimeSpan(i));
		}
		return result;
	}
	
	
	public WhRegularWorkTime getOneRegularWorkTime(int dayOfWeek){
		assert(dayOfWeek >= 1 && dayOfWeek <= 7);
		String hql = "from WhRegularWorkTime ps where ps.workday = ?";
		List values = new ArrayList();
		values.add(String.valueOf(dayOfWeek));
		WhRegularWorkTime entity = whRegularWorkTimeDao.findUnique(hql, values);
		return entity;
	}
	
	/**
	 * 获取一周中某一天的上午工作起始时间
	 * @param dayOfWeek
	 * @return
	 */
	public Time getAmStartTime(int dayOfWeek){
		assert(dayOfWeek >= 1 && dayOfWeek <= 7);
		WhRegularWorkTime entity = getOneRegularWorkTime(dayOfWeek);
		//此处表示几点几分
		return new Time(entity.getAmStartHour(), entity.getAmStartMinute());
	}
	
	/**
	 * 获取一周中某一天的上午工作结束时间
	 * @param dayOfWeek
	 * @return
	 */
	public Time getAmEndTime(int dayOfWeek){
		assert(dayOfWeek >= 1 && dayOfWeek <= 7);
		WhRegularWorkTime entity = getOneRegularWorkTime(dayOfWeek);
		//此处表示几点几分
		return new Time(entity.getAmEndHour(), entity.getAmEndMinute());
	}
	/**
	 * 获取一周中某一天的下午工作起始时间
	 * @param dayOfWeek
	 * @return
	 */
	public Time getPmStartTime(int dayOfWeek){
		assert(dayOfWeek >= 1 && dayOfWeek <= 7);
		WhRegularWorkTime entity = getOneRegularWorkTime(dayOfWeek);
		//此处表示几点几分
		return new Time(entity.getPmStartHour(), entity.getPmStartMinute());
	}
	/**
	 * 获取一周中某一天的下午午工作结束时间
	 * @param dayOfWeek
	 * @return
	 */
	public Time getPmEndTime(int dayOfWeek){
		assert(dayOfWeek >= 1 && dayOfWeek <= 7);
		WhRegularWorkTime entity = getOneRegularWorkTime(dayOfWeek);
		//此处表示几点几分
		return new Time(entity.getPmEndHour(), entity.getPmEndMinute());
	}
}
